<template>
  <div>
    <el-card style="margin: 20px 0">
      <CategroySelect @getCategroyId="getCategroyId" :show="scene != 0" />
    </el-card>
    <el-card>
      <!--底部这里有三种状态切换 -->
      <div v-show="scene == 0">
        <!-- 展示spu列表的结构 -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!category3Id"
          @click="addSpu"
          >添加SPU</el-button
        >
        <el-table border :data="records">
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="80"
          ></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column label="SPU描述" prop="description"></el-table-column>
          <el-table-column label="操作" prop="prop">
            <template slot-scope="{ row, $index }">
              <el-button
                type="success"
                icon="el-icon-plus"
                size="mini"
                title="添加sku"
                @click="addSku(row)"
              ></el-button>
              <el-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                title="修改spu"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                type="info"
                icon="el-icon-info"
                size="mini"
                title="查看当前spu全部sku列表"
                @click="hande(row)"
              ></el-button>
              <el-popconfirm
                title="这一段内容确定删除？"
                @onConfirm="deleteSpu(row)"
              >
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除spu"
                  slot="reference"
                ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          style="text-align: center"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          layout="prev,pager,next,jumper,->,sizes,total"
          :total="total"
          @current-change="handler1"
          @size-change="handler2"
        >
        </el-pagination>
      </div>
      <!-- 自定义事件 -->
      <SpuForm
        v-show="scene == 1"
        @changeScene="changeScene"
        ref="spu"
      ></SpuForm>
      <SkuForm
        v-show="scene == 2"
        ref="sku"
        @changeScenes="changeScenes"
      ></SkuForm>
    </el-card>
    <el-dialog
      :title="`${spu.spuName}的sku列表`"
      :visible.sync="dialogTableVisible"
      :before-close="close"
    >
      <!-- tab展示数据 -->
      <el-table :data="skuList" border v-loading="loading">
        <el-table-column label="名称" prop="skuName"></el-table-column>
        <el-table-column label="价格" prop="price"></el-table-column>
        <el-table-column label="重量" prop="weight"></el-table-column>
        <el-table-column label="默认图片">
          <template slot-scope="{ row, $index }">
            <img
              :src="row.skuDefaultImg"
              alt=""
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
//引入子组件
import SpuForm from "./SpuForm";
import SkuForm from "./SkuForm";
import CategroySelect from "@/components/CategroySelect";
export default {
  name: "Spu",
  components: { CategroySelect, SpuForm, SkuForm },
  data() {
    return {
      // 初始化数据  分别是分类的id
      category1Id: "",
      category2Id: "",
      category3Id: "",
      //控制三级联动的可操作性
      // show: true,
      page: 1, //分页器当前是第几页
      limit: 3, //每一页展示多少条数据
      records: [], //spu列表数据
      total: 0, //分页器一个需要展示数据的条数
      scene: 0, //0代表展示spu列表结构 1代表添加spu|修改sku 2代表添加sku
      dialogTableVisible: false, //控制对话框
      spu: {},
      skuList: [], //sku列表的数据
      loading: true, //loading加载效果
    };
  },
  methods: {
    //三级联动的自定义事件：可以把子组件相应的id传递给父组件
    getCategroyId({ categoryId, level }) {
      //categoryid：获取到一级，二级，三级分类的id
      //level：为了区分是几级id
      if (level == 1) {
        this.category1Id = categoryId;
        //清除2，3级分类的id
        this.category2Id = "";
        this.category3Id = "";
      } else if (level == 2) {
        this.category2Id = categoryId;
        //清除第三级分类的id
        this.category3Id = "";
      } else {
        this.category3Id = categoryId;
        //发请求获取spu的数据进行展示
        this.getSpuList();
      }
    },
    //获取spu列表数据的方法
    async getSpuList() {
      const { page, limit, category3Id } = this;
      //需要携带三个参数：page，limit，三级分类的id
      let result = await this.$API.spu.reqSpuList(page, limit, category3Id);
      // console.log(result);
      if (result.code == 200) {
        this.records = result.data.records;
        this.total = result.data.total;
      }
    },
    //点击分页器 当前页发生变化的回调
    handler1(page) {
      //获取最新的当前页赋值
      this.page = page;
      //重新发请求获取列表展示
      this.getSpuList();
    },
    //点击分页器条数发生变化的回调
    handler2(limit) {
      //修改数据
      this.limit = limit;
      //重新发请求获取数据展示
      this.getSpuList();
    },
    //点击添加spu按钮的回调
    addSpu() {
      this.scene = 1;
      //调用子组件的方法
      this.$refs.spu.addSpuDate(this.category3Id);
    },
    //修改spu按钮的回调
    updateSpu(row) {
      this.scene = 1;
      //获得并调用子组件spuform的方法，把需要修改spu传递过去
      this.$refs.spu.initSpuData(row);
    },
    //自定义事件的回调（spuform组件传递数据 ）
    changeScene({ scene, flag }) {
      //切换场景结构
      this.scene = scene;
      //子组件通知父组件切换场景，需要再次获取spu列表数据展示，停留在当前页
      if (flag == "修改") {
        this.getSpuList(this.page);
      } else {
        this.getSpuList(1);
      }
    },
    //删除spu
    async deleteSpu(row) {
      let result = await this.$API.spu.reqDeleteSpu(row.id);
      if (result.code == 200) {
        this.$message({ type: "success", message: "删除成功" });
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1);
      }
    },
    //添加sku按钮的回调
    addSku(row) {
      this.scene = 2;
      //父组件调用子组件的方法，让子组件发请求 三个请求
      this.$refs.sku.getData(this.category1Id, this.category2Id, row);
    },
    changeScenes(scene) {
      //切换场景
      this.scene = scene;
    },
    //查看sku按钮的回调
    async hande(spu) {
      // 对话框显示
      this.dialogTableVisible = true;
      //保存spu信息
      this.spu = spu;
      let result = await this.$API.spu.reqSkuList(spu.id);
      // console.log(result);
      if (result.code == 200) {
        this.skuList = result.data;
        this.loading = false; //关闭loading效果
      }
    },
    //关闭对话框的回调
    close(done) {
      // loading属性为真
      this.loading = true;
      //清除sku列表数据
      this.skuList = [];
      //关闭对话框
      done();
    },
  },
};
</script>

<style>
</style>